﻿using UnityEngine;
using System.Collections;

public class UtilCameraFollowVehicle : MonoBehaviour {

	public GameObject target;
	//public Transform targetFindRoot;
	/*public bool changeTarget = false;
	public float changeTargetAutoTime = 15;
	public float changeTargetAutoCounter = 0;*/

	public Vector3 offset = (new Vector3(0,0,0));
	public Vector3 focus = (new Vector3(0,1,0));
	public float vforward = -6.0f;
	public float vup = 0.0f;
	public float vright = 0.0f;
	public float height = 3.0f;

	/*public void findNewTarget() {
		Vehicle[] vs = targetFindRoot.GetComponentsInChildren<Vehicle> ();
		int n = 0;
		foreach (Vehicle v in vs) {
			if (n * Random.Range (0, n) == 0) target = v.body.gameObject; n++;
		}
	}*/

	void Update () {
		/*
		if (changeTargetAutoTime > 0)
			changeTargetAutoCounter += Time.deltaTime / changeTargetAutoTime;
		if (changeTargetAutoCounter >= 1) {
			changeTargetAutoCounter--;
			changeTarget = true;
		}

		if (!target || changeTarget || !target.activeInHierarchy) {
			findNewTarget ();
			if (!target) return;
			changeTarget = false;
		}*/
		if (target != null) {
			transform.position = (target.transform.position + vforward * target.transform.forward + vup * target.transform.up + vright * target.transform.right);
			transform.position = new Vector3 (transform.position.x, height, transform.position.z);

			transform.LookAt (target.transform.position + focus, Vector3.up);
		}
	}

}
